
//------------------------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.Linq;
using Apps.Models;
using Apps.Common;
using Microsoft.Practices.Unity;
using System.Transactions;
using Apps.BLL.Core;
using Apps.Locale;
using LinqToExcel;
using System.IO;
using System.Text;
using Apps.IDAL.MIS;
using Apps.Models.MIS;
using Apps.IBLL.MIS;
namespace Apps.BLL.MIS
{
	public partial class MIS_ArticleBLL: Virtual_MIS_ArticleBLL,IMIS_ArticleBLL
	{
        

	}
	public class Virtual_MIS_ArticleBLL
	{
        [Dependency]
        public IMIS_ArticleRepository m_Rep { get; set; }

		public virtual List<MIS_ArticleModel> GetList(ref GridPager pager, string queryStr)
        {

            IQueryable<MIS_Article> queryData = null;
            if (!string.IsNullOrWhiteSpace(queryStr))
            {
                queryData = m_Rep.GetList(
								a=>a.Id.Contains(queryStr)
								
								|| a.CategoryId.Contains(queryStr)
								|| a.Title.Contains(queryStr)
								|| a.ImgUrl.Contains(queryStr)
								|| a.BodyContent.Contains(queryStr)
								
								
								
								|| a.Checker.Contains(queryStr)
								
								|| a.Creater.Contains(queryStr)
								
								|| a.CustomLink.Contains(queryStr)
								
								);
            }
            else
            {
                queryData = m_Rep.GetList();
            }
            pager.totalRows = queryData.Count();
            //排序
            queryData = LinqHelper.SortingAndPaging(queryData, pager.sort, pager.order, pager.page, pager.rows);
            return CreateModelList(ref queryData);
        }

		public virtual List<MIS_ArticleModel> GetListByParentId(ref GridPager pager, string queryStr,object parentId)
        {
			return new List<MIS_ArticleModel>();
		}

        public virtual List<MIS_ArticleModel> CreateModelList(ref IQueryable<MIS_Article> queryData)
        {

            List<MIS_ArticleModel> modelList = (from r in queryData
                                              select new MIS_ArticleModel
                                              {
													Id = r.Id,
													ChannelId = r.ChannelId,
													CategoryId = r.CategoryId,
													Title = r.Title,
													ImgUrl = r.ImgUrl,
													BodyContent = r.BodyContent,
													Sort = r.Sort,
													Click = r.Click,
													CheckFlag = r.CheckFlag,
													Checker = r.Checker,
													CheckDateTime = r.CheckDateTime,
													Creater = r.Creater,
													CreateTime = r.CreateTime,
													CustomLink = r.CustomLink,
													IsType = r.IsType,
          
                                              }).ToList();

            return modelList;
        }

        public virtual bool Create(ref ValidationErrors errors, MIS_ArticleModel model)
        {
            try
            {
                MIS_Article entity = m_Rep.GetById(model.Id);
                if (entity != null)
                {
                    errors.Add(Resource.PrimaryRepeat);
                    return false;
                }
                entity = new MIS_Article();
               				entity.Id = model.Id;
				entity.ChannelId = model.ChannelId;
				entity.CategoryId = model.CategoryId;
				entity.Title = model.Title;
				entity.ImgUrl = model.ImgUrl;
				entity.BodyContent = model.BodyContent;
				entity.Sort = model.Sort;
				entity.Click = model.Click;
				entity.CheckFlag = model.CheckFlag;
				entity.Checker = model.Checker;
				entity.CheckDateTime = model.CheckDateTime;
				entity.Creater = model.Creater;
				entity.CreateTime = model.CreateTime;
				entity.CustomLink = model.CustomLink;
				entity.IsType = model.IsType;
  

                if (m_Rep.Create(entity))
                {
                    return true;
                }
                else
                {
                    errors.Add(Resource.InsertFail);
                    return false;
                }
            }
            catch (Exception ex)
            {
                errors.Add(ex.Message);
                ExceptionHander.WriteException(ex);
                return false;
            }
        }



         public virtual bool Delete(ref ValidationErrors errors, object id)
        {
            try
            {
                if (m_Rep.Delete(id) == 1)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            catch (Exception ex)
            {
                errors.Add(ex.Message);
                ExceptionHander.WriteException(ex);
                return false;
            }
        }

        public virtual bool Delete(ref ValidationErrors errors, object[] deleteCollection)
        {
            try
            {
                if (deleteCollection != null)
                {
                    using (TransactionScope transactionScope = new TransactionScope())
                    {
                        if (m_Rep.Delete(deleteCollection) == deleteCollection.Length)
                        {
                            transactionScope.Complete();
                            return true;
                        }
                        else
                        {
                            Transaction.Current.Rollback();
                            return false;
                        }
                    }
                }
                return false;
            }
            catch (Exception ex)
            {
                errors.Add(ex.Message);
                ExceptionHander.WriteException(ex);
                return false;
            }
        }

		
       

        public virtual bool Edit(ref ValidationErrors errors, MIS_ArticleModel model)
        {
            try
            {
                MIS_Article entity = m_Rep.GetById(model.Id);
                if (entity == null)
                {
                    errors.Add(Resource.Disable);
                    return false;
                }
                              				entity.Id = model.Id;
				entity.ChannelId = model.ChannelId;
				entity.CategoryId = model.CategoryId;
				entity.Title = model.Title;
				entity.ImgUrl = model.ImgUrl;
				entity.BodyContent = model.BodyContent;
				entity.Sort = model.Sort;
				entity.Click = model.Click;
				entity.CheckFlag = model.CheckFlag;
				entity.Checker = model.Checker;
				entity.CheckDateTime = model.CheckDateTime;
				entity.Creater = model.Creater;
				entity.CreateTime = model.CreateTime;
				entity.CustomLink = model.CustomLink;
				entity.IsType = model.IsType;
 


                if (m_Rep.Edit(entity))
                {
                    return true;
                }
                else
                {
                    errors.Add(Resource.NoDataChange);
                    return false;
                }

            }
            catch (Exception ex)
            {
                errors.Add(ex.Message);
                ExceptionHander.WriteException(ex);
                return false;
            }
        }

      

        public virtual MIS_ArticleModel GetById(object id)
        {
            if (IsExists(id))
            {
                MIS_Article entity = m_Rep.GetById(id);
                MIS_ArticleModel model = new MIS_ArticleModel();
                              				model.Id = entity.Id;
				model.ChannelId = entity.ChannelId;
				model.CategoryId = entity.CategoryId;
				model.Title = entity.Title;
				model.ImgUrl = entity.ImgUrl;
				model.BodyContent = entity.BodyContent;
				model.Sort = entity.Sort;
				model.Click = entity.Click;
				model.CheckFlag = entity.CheckFlag;
				model.Checker = entity.Checker;
				model.CheckDateTime = entity.CheckDateTime;
				model.Creater = entity.Creater;
				model.CreateTime = entity.CreateTime;
				model.CustomLink = entity.CustomLink;
				model.IsType = entity.IsType;
 
                return model;
            }
            else
            {
                return null;
            }
        }


		 /// <summary>
        /// 校验Excel数据,这个方法一般用于重写校验逻辑
        /// </summary>
        public virtual bool CheckImportData(string fileName, List<MIS_ArticleModel> list,ref ValidationErrors errors )
        {
          
            var targetFile = new FileInfo(fileName);

            if (!targetFile.Exists)
            {

                errors.Add("导入的数据文件不存在");
                return false;
            }

            var excelFile = new ExcelQueryFactory(fileName);

            //对应列头
			 				 excelFile.AddMapping<MIS_ArticleModel>(x => x.ChannelId, "ChannelId");
				 excelFile.AddMapping<MIS_ArticleModel>(x => x.CategoryId, "CategoryId");
				 excelFile.AddMapping<MIS_ArticleModel>(x => x.Title, "Title");
				 excelFile.AddMapping<MIS_ArticleModel>(x => x.ImgUrl, "ImgUrl");
				 excelFile.AddMapping<MIS_ArticleModel>(x => x.BodyContent, "BodyContent");
				 excelFile.AddMapping<MIS_ArticleModel>(x => x.Sort, "Sort");
				 excelFile.AddMapping<MIS_ArticleModel>(x => x.Click, "Click");
				 excelFile.AddMapping<MIS_ArticleModel>(x => x.CheckFlag, "CheckFlag");
				 excelFile.AddMapping<MIS_ArticleModel>(x => x.Checker, "Checker");
				 excelFile.AddMapping<MIS_ArticleModel>(x => x.CheckDateTime, "CheckDateTime");
				 excelFile.AddMapping<MIS_ArticleModel>(x => x.Creater, "Creater");
				 excelFile.AddMapping<MIS_ArticleModel>(x => x.CreateTime, "CreateTime");
				 excelFile.AddMapping<MIS_ArticleModel>(x => x.CustomLink, "CustomLink");
				 excelFile.AddMapping<MIS_ArticleModel>(x => x.IsType, "IsType");
 
            //SheetName
            var excelContent = excelFile.Worksheet<MIS_ArticleModel>(0);
            int rowIndex = 1;
            //检查数据正确性
            foreach (var row in excelContent)
            {
                var errorMessage = new StringBuilder();
                var entity = new MIS_ArticleModel();
						 				  entity.Id = row.Id;
				  entity.ChannelId = row.ChannelId;
				  entity.CategoryId = row.CategoryId;
				  entity.Title = row.Title;
				  entity.ImgUrl = row.ImgUrl;
				  entity.BodyContent = row.BodyContent;
				  entity.Sort = row.Sort;
				  entity.Click = row.Click;
				  entity.CheckFlag = row.CheckFlag;
				  entity.Checker = row.Checker;
				  entity.CheckDateTime = row.CheckDateTime;
				  entity.Creater = row.Creater;
				  entity.CreateTime = row.CreateTime;
				  entity.CustomLink = row.CustomLink;
				  entity.IsType = row.IsType;
 
                //=============================================================================
                if (errorMessage.Length > 0)
                {
                    errors.Add(string.Format(
                        "第 {0} 列发现错误：{1}{2}",
                        rowIndex,
                        errorMessage,
                        "<br/>"));
                }
                list.Add(entity);
                rowIndex += 1;
            }
            if (errors.Count > 0)
            {
                return false;
            }
            return true;
        }

        /// <summary>
        /// 保存数据
        /// </summary>
        public virtual void SaveImportData(IEnumerable<MIS_ArticleModel> list)
        {
            try
            {
                using (DBContainer db = new DBContainer())
                {
                    foreach (var model in list)
                    {
                        MIS_Article entity = new MIS_Article();
                       						entity.Id = ResultHelper.NewId;
						entity.ChannelId = model.ChannelId;
						entity.CategoryId = model.CategoryId;
						entity.Title = model.Title;
						entity.ImgUrl = model.ImgUrl;
						entity.BodyContent = model.BodyContent;
						entity.Sort = model.Sort;
						entity.Click = model.Click;
						entity.CheckFlag = model.CheckFlag;
						entity.Checker = model.Checker;
						entity.CheckDateTime = model.CheckDateTime;
						entity.Creater = model.Creater;
						entity.CreateTime = ResultHelper.NowTime;
						entity.CustomLink = model.CustomLink;
						entity.IsType = model.IsType;
 
                        db.MIS_Article.Add(entity);
                    }
                    db.SaveChanges();
                }
            }
            catch
            {
                throw;
            }
        }


        public virtual bool IsExists(object id)
        {
            return m_Rep.IsExist(id);
        }
		  public void Dispose()
        { 
            
        }

	}
}
